AddPrefabPostInit(
    "reskin_tool",
    function(inst)
        if not TheWorld.ismastersim then
            return
        end
        
        local function Change_Item_Skin(arg1,arg2)
            if arg1 == nil then
                return
            end
            local doer = arg1
            local tarItem = arg2
            if arg2 == nil then
                tarItem = arg1
                doer = TheSim:FindFirstEntityWithTag("player")
            end

            if doer == nil or doer.userid == nil or tarItem == nil then
                return
            end

            ------------------------------------------------------------------------------------------------------
            local function spellCB_next_only(target,caster)
                local tool = target
                if tool._cached_reskinname == nil then
                    tool._cached_reskinname = {}
                end
                tool:DoTaskInTime(0, function()
            
                    local prefab_to_skin = target.prefab_base or target.prefab
                    if target:IsValid()  then
                        local curr_skin = target.skinname
                        local userid = caster.userid or ""
                        local cached_skin = tool._cached_reskinname[prefab_to_skin]
                        local search_for_skin = cached_skin ~= nil --also check if it's owned
                        if curr_skin == cached_skin or (search_for_skin and not TheInventory:CheckClientOwnership(userid, cached_skin)) then
                            local new_reskinname = nil
            
                            if PREFAB_SKINS[prefab_to_skin] ~= nil then
                                for _,item_type in pairs(PREFAB_SKINS[prefab_to_skin]) do
                                    if search_for_skin then
                                        if cached_skin == item_type then
                                            search_for_skin = false
                                        end
                                    else
                                        if TheInventory:CheckClientOwnership(userid, item_type) then
                                            new_reskinname = item_type
                                            break
                                        end
                                    end
                                end
                            end
                            tool._cached_reskinname[prefab_to_skin] = new_reskinname
                            cached_skin = new_reskinname
                        end                
                        -- print(target.skinname,cached_skin)
                        TheSim:ReskinEntity( target.GUID, target.skinname, cached_skin, nil, userid )
                        if cached_skin == nil then
                            if target.components.inventoryitem then
                                target.components.inventoryitem:ChangeImageName(target.prefab_base or target.prefab)
                            end
                        end

                    end
                end)
            end
            local flag,error_code = pcall(spellCB_next_only,tarItem,doer)
            if flag == false then
                print("Error : Reskin Next")
                print(error_code)
            end            
            ------------------------------------------------------------------------------------------------------

        end

        inst.components.spellcaster.can_cast_fn__old_npc = inst.components.spellcaster.can_cast_fn
        inst.components.spellcaster.can_cast_fn = function(doer, target, pos)
            if target and target:HasTag("npc_item_skin") then
                return true
            end
            return inst.components.spellcaster.can_cast_fn__old_npc(doer,target,pos)
        end


        inst.components.spellcaster.spell__old_npc = inst.components.spellcaster.spell
        inst.components.spellcaster.spell = function(tool, target, pos, caster)
            if target and target:HasTag("npc_item_skin") then
                Change_Item_Skin(caster,target)
                return
            end
            inst.components.spellcaster.spell__old_npc(tool, target, pos, caster)
        end
        
    end
)